{% extends "submission/info-base.html" %}
{% block js_media %}
    <script type="text/javascript" src="{{ static('event.js') }}"></script>
    {% if not submission.is_graded and last_msg %}
        <script type="text/javascript">
            $(function () {
                var blocked = false, request = false;
                var list = $('#test-cases');

                function update() {
                    if (blocked) {
                        request = true;
                        return;
                    }
                    request = false;
                    blocked = true;
                    $.ajax({
                        url: '{{ url('submission_testcases_query') }}',
                        data: {id: '{{ submission.id }}'}
                    }).done(function (data) {
                        list.empty().html(data).find('.toggle').each(function () {
                            register_toggle($(this));
                        });
                        setTimeout(function () {
                            blocked = false;
                            if (request)
                                update();
                        }, 500);
                    }).fail(function (data) {
                        console.log('Failed to update testcases!');
                    });

                    if ($(window).scrollTop() + $(window).height() > $(document).height() - 100)
                        $("html, body").animate({scrollTop: $(document).height()}, 0);
                }

                var receiver = new EventReceiver(
                    "{{ EVENT_DAEMON_LOCATION }}", "{{ EVENT_DAEMON_POLL_LOCATION }}",
                    ['sub_{{ submission.id_secret }}'], {{ last_msg }}, function (message) {
                        switch (message.type) {
                            case 'aborted':
                            case 'internal-error':
                            case 'grading-end':
                            case 'compile-error':
                                $('#abort-button').remove();
                                $('#grading-label').remove();
                            case 'test-case':
                            case 'grading-begin':
                            case 'processing':
                                update();
                        }
                    }
                )
            });
        </script>
    {% endif %}
    {% if not submission.is_graded %}
        <script type="text/javascript">
            $(function () {
                $(document).keydown(function(e) {
                    // Ctrl-Enter or Command-Enter
                    if ((e.metaKey || e.ctrlKey) && e.which == 13) {
                        $('#abort-button form').submit();
                    }
                });
            });
        </script>
    {% endif %}
{% endblock %}

{% block body %}
    <br>
    <div><a href="{{ url('submission_source', submission.id) }}">{{ _('View source') }}</a></div>
    {% if request.user == submission.user.user or perms.judge.resubmit_other %}
        <div><a href="{{ url('problem_submit', submission.problem.code, submission.id) }}">{{ _('Resubmit') }}</a></div>
    {% endif %}
    {% if perms.judge.rejudge_submission and not submission.is_locked %}
        {% compress js %}
            <script type="text/javascript">
                window.confirm_and_rejudge = function (form) {
                    if (confirm({{ _('Are you sure you want to rejudge?')|htmltojs }})) {
                        form.submit();
                    }
                };
            </script>
        {% endcompress %}
        <div>
            <form action="{{ url('submission_rejudge') }}" method="post">
                {% csrf_token %}
                <a href="#" onclick="confirm_and_rejudge(parentNode)">{{ _('Rejudge') }}</a>
                <input type="hidden" name="id" value="{{ submission.id }}">
                <input type="hidden" name="path" value="{{ url('submission_status', submission.id) }}">
            </form>
        </div>
    {% endif %}

    <br>
    <hr class="half-hr">
    <br>

    <div id="test-cases">{% include "submission/status-testcases.html" %}</div>

    {% if not submission.is_graded %}
        {% if request.user == submission.user.user or perms.judge.abort_any_submission %}
            <div id="abort-button">
                <br>
                <hr class="half-hr">
                <br>
                <form action="{{ url('submission_abort', submission.id) }}" method="post"
                      title="{{ _('Press Ctrl-Enter or Command-Enter to abort.') }}">
                    {% csrf_token %}
                    <input style="float:left" type="submit" value="{{ _('Abort') }}" class="button">
                    <br><br>
                </form>
            </div>
        {% endif %}
    {% endif %}
{% endblock %}
